package xin.clips.web.controller.gy;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import xin.clips.common.annotation.Log;
import xin.clips.common.annotation.VisistLog;
import xin.clips.common.enums.BusinessType;
import xin.clips.common.core.controller.BaseController;
import xin.clips.common.core.domain.AjaxResult;
import xin.clips.common.enums.gy.DeviceTypeEnum;
import xin.clips.common.utils.poi.ExcelUtil;
import xin.clips.common.core.page.TableDataInfo;
import xin.clips.system.domain.gy.*;
import xin.clips.system.service.gy.IGyArmParamRecordService;
import xin.clips.system.service.gy.IGyDeviceRecordService;
import xin.clips.system.service.gy.IGyMachineParamRecordService;

/**
 * 设备历史记录管理Controller
 *
 * @author clips
 * @date 2023-03-15
 */
@Controller
@RequestMapping("/gy/gyDeviceRecord")
public class GyDeviceRecordController extends BaseController {
    private String prefix = "gy/gyDeviceRecord";

    @Autowired
    private IGyDeviceRecordService gyDeviceRecordService;

    @Autowired
    private IGyArmParamRecordService armParamRecordService;

    @Autowired
    private IGyMachineParamRecordService machineParamRecordService;

    @RequiresPermissions("gy:gyDeviceRecord:view")
    @VisistLog(title = "设备历史记录管理")
    @GetMapping()
    public String gyDeviceRecord() {
        return prefix + "/gyDeviceRecord";
    }

    /**
     * 查询备历史记录管理列表
     */
    @RequiresPermissions("gy:gyDeviceRecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GyDeviceRecord gyDeviceRecord) {
        startPage();
        List<GyDeviceRecord> list = gyDeviceRecordService.selectGyDeviceRecordList(gyDeviceRecord);
        return getDataTable(list);
    }

    /**
     * 导出备历史记录管理列表
     */
    @RequiresPermissions("gy:gyDeviceRecord:export")
    @Log(title = "设备历史记录管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GyDeviceRecord gyDeviceRecord) {
        List<GyDeviceRecord> list = gyDeviceRecordService.selectGyDeviceRecordList(gyDeviceRecord);
        ExcelUtil<GyDeviceRecord> util = new ExcelUtil<GyDeviceRecord>(GyDeviceRecord.class);
        return util.exportExcel(list, "备历史记录管理数据");
    }

    /**
     * 新增备历史记录管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存备历史记录管理
     */
    @RequiresPermissions("gy:gyDeviceRecord:add")
    @Log(title = "设备历史记录管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GyDeviceRecord gyDeviceRecord) {
        return toAjax(gyDeviceRecordService.insertGyDeviceRecord(gyDeviceRecord));
    }

    /**
     * 修改备历史记录管理
     */
    @RequiresPermissions("gy:gyDeviceRecord:edit")
    @GetMapping("/edit/{recordId}")
    public String edit(@PathVariable("recordId") Long recordId, ModelMap mmap) {
        GyDeviceRecord gyDeviceRecord = gyDeviceRecordService.selectGyDeviceRecordByRecordId(recordId);
        mmap.put("gyDeviceRecord", gyDeviceRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存备历史记录管理
     */
    @RequiresPermissions("gy:gyDeviceRecord:edit")
    @Log(title = "设备历史记录管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GyDeviceRecord gyDeviceRecord) {
        return toAjax(gyDeviceRecordService.updateGyDeviceRecord(gyDeviceRecord));
    }

    /**
     * 删除备历史记录管理
     */
    @RequiresPermissions("gy:gyDeviceRecord:remove")
    @Log(title = "设备历史记录管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(gyDeviceRecordService.deleteGyDeviceRecordByRecordIds(ids));
    }


    /**
     * 详情
     */
    @GetMapping("/detail")
    public String detail(@RequestParam("recordId") String recordId, ModelMap mmap) {
        GyDeviceRecord deviceRecord = gyDeviceRecordService.selectGyDeviceRecordByRecordId(Long.valueOf(recordId));
        GyMachineParamRecord machineParam = new GyMachineParamRecord();
        GyArmParamRecord armParam = new GyArmParamRecord();
        if (deviceRecord.getDeviceType().equals(DeviceTypeEnum.MACHINE.getInfo())) {
            machineParam = machineParamRecordService.selectGyMachineParamRecordByRecordId(deviceRecord.getRecordId());
        } else if (deviceRecord.getDeviceType().equals(DeviceTypeEnum.ARM.getInfo())) {
            armParam = armParamRecordService.selectGyArmParamRecordByRecordId(deviceRecord.getRecordId());
        }
        mmap.put("device", deviceRecord);
        mmap.put("armParam", armParam);
        mmap.put("machineParam", machineParam);
        return prefix + "/detail";
    }
}
